/**
 * @file FreeRTOS_app.c
 * @author liufeng (liufeng@injoinic.com)
 * @brief FreeRTOS task.
 * @version 0.1
 * @date 2024-11-07
 *
 * @copyright Copyright (c) 2024  injoinic
 *
 */

#include "FreeRTOS_app.h"
#include "FreeRTOS.h"
#include "task.h"

TaskHandle_t StartTask_Handle = NULL; // 用于任务创建
TaskHandle_t TestTask_Handle = NULL;

void StartTask(void* argument);
void TestTask(void* argument);

void FreeRTOS_app()
{
    BaseType_t xReturn = pdPASS;
    xReturn = xTaskCreate(StartTask, "StartTask", 128, NULL, 1, &StartTask_Handle);
    configASSERT(xReturn);

    vTaskStartScheduler();
}

/* -------------------------------------------------------------------------- */
/*                                   start任务                                 */
/* -------------------------------------------------------------------------- */

void StartTask(void* argument)
{
    taskENTER_CRITICAL(); // 进入临界区

    /* 创建TestTask */
    BaseType_t xReturn = pdPASS;
    xReturn = xTaskCreate(TestTask, "TestTask", 128, NULL, 1, &TestTask_Handle);
    configASSERT(xReturn);

    vTaskDelete(StartTask_Handle); // 使命完成，删除

    taskEXIT_CRITICAL();           // 退出临界区
}

/* -------------------------------------------------------------------------- */
/*                                   测试任务                                  */
/* -------------------------------------------------------------------------- */

void TestTask(void* argument)
{
    while (1) {
        int num = 10;
        int* pnum = (int*)0xffffffff;
        int res = num / (*pnum);
        printf("%d\r\n", res);
        vTaskDelay(1000);
    }
}
